package com.giantest.ink;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 128M Ink 点除rule
 * 具体点除规则
 */
public class InkRule {

    /**
     * 点除规则1
     * 计算周围8个点的得分
     * @param arr
     */
    public static void rule1(char[][] arr, Set<String> coordinate) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                // score1 score2 score3
                // score4 判断点 score6
                // score7 score8 score9
                // 判断点=1的时候，计算周围一圈点的评分
                if (arr[i][j] == '1') {
                    int score1 = Rule1Score.score1(i, j, arr);
                    int score2 = Rule1Score.score2(i, j, arr);
                    int score3 = Rule1Score.score3(i, j, arr);
                    int score4 = Rule1Score.score4(i, j, arr);
                    int score6 = Rule1Score.score6(i, j, arr);
                    int score7 = Rule1Score.score7(i, j, arr);
                    int score8 = Rule1Score.score8(i, j, arr);
                    int score9 = Rule1Score.score9(i, j, arr);
                    int scoreAll = score1+score2+score3+score4+score6+score7+score8+score9;
                    if (scoreAll >= 8) {
                        //评分大于等于8 该点需要进行点除
                        coordinate.add(i+"_"+j);
                    }
                }
            }
        }
    }

    /**
     * 点除规则2
     * 判断是否连续50个点fail
     * @param arr
     * @param coordinate
     */
    public static void rule2(char[][] arr, Set<String> coordinate) {
        //检测过的坐标不再检测
        Set<String> checked = new HashSet<String>();
        //用来存放连续递归的坐标集
        List<Set<String>> list = new ArrayList<Set<String>>();
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (!checked.contains(i+"_"+j)) {
                    if (arr[i][j] != '.' && arr[i][j] != '1' && arr[i][j] != ' ') {
                        Set<String> failed = new HashSet<String>();
                        radiation(i, j, arr, checked, failed);
                        list.add(failed);
                    } else {
                        checked.add(i+"_"+j);
                    }
                }
            }
        }
//        System.out.println(list);
        for (Set<String> set : list) {
//            System.out.println(set.size());
            if (set.size() >= 50) {
                coordinate.addAll(set);
            }
        }
    }

    /**
     * 判断的failed的点，向外辐射进行递归判断
     * @param i
     * @param j
     * @param arr
     * @param checked
     * @param failed
     */
    public static void radiation(int i, int j, char[][] arr, Set<String> checked, Set<String> failed) {
        if (!checked.contains(i+"_"+j)) {
            checked.add(i+"_"+j);
            if (arr[i][j] != '.' && arr[i][j] != '1' && arr[i][j] != ' ') {
                failed.add(i+"_"+j);
                for (int k = i-1; k <= i+1; k++) {
                    for (int l = j-1; l <= j+1; l++) {
                        if (k >= 0 && k < arr.length && l >=0 && l < arr[k].length) {
                            //递归
                            radiation(k, l, arr, checked, failed);
                        }
                    }
                }
            }
        }
    }

}
